package com.yzt.zhuju.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import androidx.fragment.app.FragmentActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class ActivityManagerUtils {

    public static ActivityManagerUtils instance;

    public static List<FragmentActivity> activityList = new LinkedList<>();

    public FragmentActivity currentActivity;

    private Activity activity;

    public ActivityManagerUtils() {

    }

    public static ActivityManagerUtils getInstance() {
        if (null == instance) {
            instance = new ActivityManagerUtils();
        }
        return instance;
    }

    public void addActivity(FragmentActivity activity) {
        if (activity != null) {
            activityList.add(activity);
        }
    }

    public void remove(FragmentActivity activity) {
        if (activity != null) {
            activityList.remove(activity);
        }
    }

    public void remove(String activityName) {
        for (Activity activity : activityList) {
            if (activity.getClass().getSimpleName().equals(activityName)) {
                activityList.remove(activity);
            }
        }
    }

    public void finishActivity(String className) {
        for (Activity activity : activityList) {
            if (activity.getClass().getName().equals(className)) {
                activity.finish();
            }
        }
    }

    public FragmentActivity getTopActivity() {
        if (activityList != null && activityList.size() > 0) {
            int index = activityList.size() - 1;
            while (index >= 0) {
                FragmentActivity fragmentActivity = activityList.get(index);
                if (fragmentActivity.isFinishing() || fragmentActivity.isDestroyed()) {
                    index--;
                } else {
                    return fragmentActivity;
                }
            }
        }
        return null;
    }

    public FragmentActivity permissionGetTopActivity() {
        if (activityList != null && activityList.size() > 0) {
            int index = activityList.size() - 2;
            while (index >= 0) {
                FragmentActivity fragmentActivity = activityList.get(index);
                if (fragmentActivity.isFinishing() || fragmentActivity.isDestroyed()) {
                    index--;
                } else {
                    return fragmentActivity;
                }
            }
        }
        return null;
    }

    /**
     * 是否有打开的activity
     */
    public boolean haveActivity(String name) {
        if (!haveActivity()) {
            return false;
        }

        for (FragmentActivity activity : activityList) {
            if (activity.getClass().getSimpleName().equals(name)) {
                return true;
            }
        }
        return false;
    }

    public Activity findActivity(Class<? extends Activity> clz) {
        for (FragmentActivity activity : activityList) {
            if (activity.getClass().equals(clz)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 是否有打开的activity
     */
    public boolean haveActivity() {
        return (activityList != null && activityList.size() > 0);
    }

    public String getTopActivity(Context ctx) {
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
        if (tasksInfo.size() > 0) {
            return ctx.getPackageName();
        }
        return "";
    }

    public void exitApp(int type) {
        finishAllActivity();
        if (type == 1) {// 异常退出
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);// 非0参数都可以非正常的方式结束虚拟机
        } else {// 正常退出
            System.exit(0);// 正常退出
        }
    }

    public void finishAllActivity() {
        if (activityList != null) {
            for (Activity activity : activityList) {
                if (activity != null) {
                    activity.finish();
                }
            }
        }
        activityList.clear();
    }

    public FragmentActivity getCurrentActivity() {
        return currentActivity;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
